﻿using System.Xml;
using System.Xml.Serialization;

namespace CloudShot.Core.Utils
{
	public static class XmlExtensions
	{
		public static readonly XmlSerializerNamespaces EmptyXmlSerializerNamespace = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("") });

		public static void WriteElementObject(this XmlWriter writer, string localName, object o)
		{
			writer.WriteStartElement(localName);
			var xs = new XmlSerializer(o.GetType());
			xs.Serialize(writer, o, EmptyXmlSerializerNamespace);
			writer.WriteEndElement();
		}

		public static T ReadElementObject<T>(this XmlReader reader)
		{
			var xs = new XmlSerializer(typeof(T));
			reader.ReadStartElement();
			T retval = (T)xs.Deserialize(reader);
			reader.ReadEndElement();
			return retval;
		}
	}
}